package tron.config;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.jsontype.impl.LaissezFaireSubTypeValidator;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

/**
 * @description redis配置类
 * @auther TRON
 */
@Profile("sentinel")
@Configuration
public class TronRedisSentinelConfig {

    private RedisConfigProperties redisConfigProperties;

    public TronRedisSentinelConfig(RedisConfigProperties redisConfigProperties) {
        this.redisConfigProperties = redisConfigProperties;
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate redisTemplate = new RedisTemplate();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        //创建自定义序列化器
        ObjectMapper om = new ObjectMapper();
        //不使用默认的dateTime进行序列化
        om.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        //序列化LocalDateTIme和LocalDate的必要配置,由Jackson-data-JSR310实现
        om.registerModule(new JavaTimeModule());
        om.registerModule((new SimpleModule()));
        om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
        GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer(om);
        //设置默认的Serialize，包含 keySerializer & valueSerializer
        redisTemplate.setDefaultSerializer(genericJackson2JsonRedisSerializer);
        return redisTemplate;
    }

    @Bean
    public RedisConnectionFactory redisConnectionFactory() {
        final JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisSentinelConfiguration(),jedisClientConfiguration());
        return jedisConnectionFactory;
    }



    @Bean
    public RedisSentinelConfiguration redisSentinelConfiguration() {
        final RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        redisSentinelConfiguration.setPassword(redisConfigProperties.getPassword().toString());
        redisSentinelConfiguration.setMaster(redisConfigProperties.getSentinel().getMaster());
        final List<String> nodes = redisConfigProperties.getSentinel().getNodes();
        final ArrayList<RedisNode> redisNodes = new ArrayList<>();
        for (String node : nodes) {
            final String[] split = node.split(":");
            final RedisNode redisNode = new RedisNode(split[0], Integer.parseInt(split[1]));
            redisNodes.add(redisNode);
        }
        redisSentinelConfiguration.setSentinels(redisNodes);
        return redisSentinelConfiguration;
    }

    @Bean
    public JedisClientConfiguration jedisClientConfiguration() {
        final JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder = JedisClientConfiguration.builder().usePooling();
        builder.poolConfig(jedisPool());
        builder.and().readTimeout(Duration.ofSeconds(10));
        builder.and().connectTimeout(Duration.ofSeconds(10));
        return builder.build();
    }

    @Bean
    public JedisPoolConfig jedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxIdle(10);
        jedisPoolConfig.setMaxWaitMillis(60);
        jedisPoolConfig.setMaxTotal(100);
        jedisPoolConfig.setMinIdle(100);
        return jedisPoolConfig;
    }


}
